package com.ma4567.remotecontroller.UserManageRequest;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.MysqlHelper.TableLogManager;
import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.MysqlHelper.TableUserManager;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.UserManager.User;
import com.ma4567.remotecontroller.UserManager.UserManager;
import com.ma4567.remotecontroller.ffmpegManager.ffmpegController;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

@CrossOrigin
@RestController
public class UpdataUserInfo {
    @RequestMapping(value = "/api/updataUserInfo", method = RequestMethod.GET)
    public String updataUserInfo(HttpServletRequest request,
                                 @RequestParam("cookie") String Cookie,
                                 @RequestParam("userAccount") int userAccount,
                                 @RequestParam("NameOrPassword") String NameOrPassword,
                                 @RequestParam("updataItem") String updataItem) throws SQLException {
        ffmpegController.StopStream();
        TableRequestLog.AddLog("Change user " + updataItem + " with " + NameOrPassword, Cookie, request.getRemoteAddr(), userAccount);
        if(!RemoteControllerApplication.userManager.JudgeCookie(Cookie)){//鉴cookie
            return JSONObject.toJSONString(new getUserListResponse(1, null), SerializerFeature.PrettyFormat);
        }
        else {
            if(!RemoteControllerApplication.userManager.GetUserSingleItemPermission(Cookie, "Root")){//鉴权
                return JSONObject.toJSONString(new getUserListResponse(2, null), SerializerFeature.PrettyFormat);
            }
            else {
                if(NameOrPassword.length() <= 0){
                    return JSONObject.toJSONString(new getUserListResponse(4, RemoteControllerApplication.userManager.GetAllUser()), SerializerFeature.PrettyFormat);
                }

                if("Name".equals(updataItem)){
                    for(User u: TableUserManager.GetUserList()){
                        if(u.name.equals(NameOrPassword)){
                            return JSONObject.toJSONString(new getUserListResponse(3, RemoteControllerApplication.userManager.GetAllUser()), SerializerFeature.PrettyFormat);
                        }
                    }
                    RemoteControllerApplication.userManager.UpdataUserName(userAccount, NameOrPassword);
                } else if("Password".equals(updataItem)){
                    RemoteControllerApplication.userManager.UpdataUserPassword(userAccount, NameOrPassword);
                }
                TableLogManager.AddLog("Change user " + updataItem + " with " + NameOrPassword, Cookie, userAccount);
                return JSONObject.toJSONString(new getUserListResponse(0, RemoteControllerApplication.userManager.GetAllUser()), SerializerFeature.PrettyFormat);
            }
        }
    }
}
